﻿using System;
using System.Collections.Generic;
using System.Text;
using MobileBroadbandUserBlog.MBLM.Core;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;
using MobileBroadbandUserBlog.MBLM.Core.MobileSoftware;

namespace MobileBroadbandUserBlog.MBLM
{
    public class MobileBroadbandLoggingMonitor : IMobileBroadbandLoggingMonitor
    {
        private IMobileSoftwareFactory _mobileSoftwareFactory;
        public MobileBroadbandLoggingMonitor(IMobileSoftwareFactory mobileSoftwareFactory)
        {
            if (mobileSoftwareFactory == null)
            {
                throw new ArgumentNullException("mobileSoftwareFactory cannot be null.");
            }
            _mobileSoftwareFactory = mobileSoftwareFactory;
        }

        public MobileBroadbandLoggingMonitor() : this(new MobileSoftwareFactory()) {}

        #region IMobileBroadbandLoggingMonitor Members

        public override bool IsMobileSoftwarePresentOnSystem()
        {
            return GetMobileBroadbandSoftwareForThisComputer() != null;
        }

        public override IMobileSoftwareLogger GetMobileSoftwareLogger()
        {
            if (this.IsMobileSoftwarePresentOnSystem())
            {
                return this.GetMobileSoftwareLogger(GetMobileBroadbandSoftwareForThisComputer());
            }
            return null;
        }


        public override IMobileSoftwareLogger GetMobileSoftwareLogger(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(@"path cannot be null/empty.");
            }
            return this.GetMobileSoftwareLogger(new UserDefinedSoftware(path));
        }

        public override IMobileSoftwareLogger GetMobileSoftwareLogger(IMobileSoftware mobileSoftware)
        {
            if (mobileSoftware == null)
            {
                throw new ArgumentNullException("mobileSoftware cannot be null.");
            }

            return new MobileSoftwareLogger(mobileSoftware);
        }

        private IMobileSoftware GetMobileBroadbandSoftwareForThisComputer()
        {
            return DeterminePresentSoftware();
        }

        private IMobileSoftware DeterminePresentSoftware()
        {
            foreach (IMobileSoftware software in _mobileSoftwareFactory.GetMobileBroadbandSoftware())
            {
                if (software.IsPresent())
                {
                    return software;
                }
            }
            return null;
        }
        #endregion
    }
}
